home *** CD-ROM | disk | FTP | other *** search
/ Network Supervisor's Toolkit / Network Supervisor's Toolkit.iso / tools / pprd100 / jd.c < prev    next >
C/C++ Source or Header  |  1996-07-10  |  3KB  |  136 lines

  1. /*
  2.  *    JD
  3.  *
  4.  *    Send job to printer or spooler using direct protocol.
  5.  *
  6.  *    These entries in WATTCP.CFG are recognised:
  7.  *
  8.  *    PRINTERHOST = domain name or ip address of host
  9.  *    PORTBASE = number of port for LPT1, LPT2 at PORTBASE+1, etc.
  10.  *    PRINTERNUM = 0 for LPT1, 1 for LPT2, 2 for LPT3.
  11.  *
  12.  *    These can be overriden on the command line with:
  13.  *
  14.  *    -H<printerhost>
  15.  *    -B<portbase>
  16.  *    -P<printernum>
  17.  *
  18.  *    Defaults are:
  19.  *
  20.  *    PORTBASE = 9100
  21.  *    PRINTERNUM = 0
  22.  *
  23.  *    When printing a job, outputs one dot per 1k bytes.
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include "tcp.h"
  30.  
  31. #define    BUFFERSIZE    1024
  32.  
  33. char        *printerhost = "printer";
  34. int        portbase = 9100;
  35. int        printernum = 0;
  36. char        buffer[BUFFERSIZE];
  37. tcp_Socket    sock;
  38. void        (*normal_init)(char *name, char *value);
  39.  
  40. void my_init(char *name, char *value)
  41. {
  42.     if (strcmp(name, "PRINTERHOST") == 0)
  43.         printerhost = strdup(value);
  44.     else if (strcmp(name, "PORTBASE") == 0)
  45.         portbase = atoi(value);
  46.     else if (strcmp(name, "PRINTERNUM") == 0)
  47.         printernum = atoi(value);
  48.     else if (normal_init)
  49.         (*normal_init)(name, value);
  50. }
  51.  
  52. int jd(char *host, int port, FILE *file)
  53. {
  54.     longword    hostaddr;
  55.     int        localport;
  56.     int        status = 0;
  57.     int        connected = 0;
  58.     int        completed = 0;
  59.     int        i;
  60.  
  61.     if (!(hostaddr = resolve(host)))
  62.     {
  63.         (void)printf("JD: %s: Cannot get address for host\n", host);
  64.         return (1);
  65.     }
  66.     localport = 255 + (MsecClock() & 255);
  67.     if (!tcp_open(&sock, localport, hostaddr, port, NULL))
  68.     {
  69.         (void)printf("JD: Cannot connect to host\n");
  70.         return (1);
  71.     }
  72.     sock_wait_established(&sock, sock_delay, NULL, &status);
  73.     connected = 1;
  74.     (void)printf("connected");
  75.     /* dump file */
  76.     while ((i = fread(buffer, sizeof(char), BUFFERSIZE, file)) != 0)
  77.     {
  78.         sock_write(&sock, buffer, i);
  79.         (void)printf(".");
  80.         sock_tick(&sock, &status);
  81.     }
  82.     (void)printf("\n");
  83.     completed = 1;
  84.     sock_tick(&sock, &status);    /* in case they sent reset */
  85.     sock_close(&sock);
  86.     sock_wait_closed(&sock, sock_delay, NULL, &status);
  87. sock_err:
  88.     if (status == -1)
  89.         (void)printf("JD: Remote host reset connection\n");
  90.     if (!connected)
  91.         (void)printf("JD: Could not get connected.\n");
  92.     return(!completed);
  93. }
  94.  
  95. int main(int argc, char **argv)
  96. {
  97.     char        *s;
  98.     FILE        *file;
  99.     int        status;
  100.  
  101.     /* hook onto init procedure to get NAME=VALUE pairs */
  102.     normal_init = usr_init;
  103.     usr_init = my_init;
  104.     sock_init();
  105.     for (--argc, ++argv; argc > 0 && *(s = argv[0]) == '-'; --argc, ++argv)
  106.     {
  107.         s++;
  108.         switch (*s)
  109.         {
  110.         case 'B':
  111.             portbase = atoi(++s);
  112.             break;
  113.         case 'H':
  114.             printerhost = ++s;
  115.             break;
  116.         case 'P':
  117.             printernum = atoi(++s);
  118.             break;
  119.         }
  120.     }
  121.     (void)printf("PRINTERHOST = %s PORT = %d\n", printerhost,
  122.         portbase + printernum);
  123.     if (argc <= 0)
  124.         file = stdin;
  125.     else
  126.     {
  127.         if ((file = fopen(argv[0], "rb")) == 0)
  128.         {
  129.             (void)printf("JD: %s: No such file\n", argv[0]);
  130.             return (1);
  131.         }
  132.     }
  133.     status = jd(printerhost, portbase + printernum, file);
  134.     return (status);
  135. }
  136.